class ChessBoard:
    def __init__(self, room):
        self.room = room

        self.players = []
        self.chess = {
            1: None,  # 黑棋
            0: None  # 白棋
        }
        self.now = 1
        self.__record = []
        self.init()

    def init(self):
        for user in self.room.member:
            self.players.append(user)
        self.chess[1] = self.players[0].name
        self.chess[0] = self.players[1].name

    # 获取房间棋盘记录仪
    def getRecord(self):
        return self.__record

    # 添加布棋
    def addRecord(self, **kwargs):
        tape = kwargs.get("tape")
        if (self.now != tape[0]):
            return
        self.__record.append(tape)
        self.now = 0 if self.now == 1 else 1
        for user in self.players:
            user.ws.send('chessBoard.addRecord', {
                "tape": tape,
                "now": self.now
            })

    def getInfo(self):
        return {
            "now": self.now,
            "players": [user.name for user in self.players],
            "chess": self.chess,
            "record": self.getRecord()
        }

    # 返回棋盘信息
    def info(self, **kwargs):
        name = kwargs.get('name')
        res = self.getInfo()
        for user in self.players:
            if user.name == name:
                user.ws.send('chessBoard.info', res)
